package com.gjq.springboot.controller;



import com.gjq.springboot.config.SftpConnectConfig;
import com.gjq.springboot.entity.File;
import com.gjq.springboot.entity.downDTO;
import com.gjq.springboot.entity.user;
import com.gjq.springboot.utils.FtpUtil;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.gjq.springboot.server.SftpFileService;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;



@RestController
@Slf4j
@CrossOrigin//该注解可以解决跨越问题。
public class SftpFileController {

    @Autowired
    private SftpConnectConfig sftpConnectConfig;

    @Autowired
    private SftpFileService fileService;

    /**
     * ssh登入选择服务器ip
     * @param user 服务器ip
     */
    @PostMapping("/login")
    public int login(@RequestBody user user) throws  InterruptedException {
        sftpConnectConfig.setHost(user.getIp());
        sftpConnectConfig.setUsername(user.getName());
        sftpConnectConfig.setPassword(user.getPassword());
        System.out.println(user.getName()+" "+user.getIp()+" "+user.getPassword());
        return fileService.connectSftp();
    }


    /**
     * 通过浏览器下载文件
     * @param file 文件名
     * @param path 文件在服务器的路基
     * @param response
     * @return
     */
    @GetMapping("/download")
    public ResponseEntity<byte[]> download(@RequestParam("file") String file, @RequestParam("path")String path,
                                           HttpServletResponse response) {
        System.out.println(file + " " + path);
        //设置响应信息
        byte[] fileContent = null;
        try {
            fileContent = fileService.download(path, file);
        } catch (Exception e) {
            e.printStackTrace();
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", file);


        if (fileContent == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
    }

    /**
     * 上传文件到服务器
     * @param multipartFile 要上传到服务器的文件，注意此处的path必须在结尾添加 /
     * @param path 上传到服务器的路径
     */
    @PostMapping("/upload")
    public boolean upload(@RequestParam("file") MultipartFile multipartFile, @RequestParam("selectIndex") String path){
        System.out.println(multipartFile.getSize());
        path = path.replaceAll("\"", "");
        try {
            boolean res = fileService.upload(multipartFile, path);
            return res;
        } catch (Exception e) {
            log.error("",e);
            return false;
        }
    }

    @GetMapping("/filesdatalistplatform")
    public Object getFilesList(String path){

        return fileService.getFileList(path);
    }

    @PutMapping("/addFolder")
    @ResponseBody
    public boolean addFolder(@RequestBody File file){
        //System.out.println(file.getFilename()+" "+file.getPath());
        String filename = file.getFilename();
        int len = filename.length();
        for(int i=0;i<len;i++){
            if(filename.charAt(i)==' ') return false;
        }
        String path = file.getPath().replaceAll("\"", "");
        return fileService.addFolder(file.getFilename(), path);
    }

    @GetMapping("/openFolder")
    public Object open(@RequestParam("path") String path){
        path = path.replaceAll("\"", "");
        System.out.println("open "+path);
        return fileService.getFileList(path);
    }
}
